package us.wili.dev.client.service.impl;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.wili.dev.client.dao.ClientShopCartMapper;
import us.wili.dev.client.model.Detail;
import us.wili.dev.client.model.ShopCartId;
import us.wili.dev.client.model.req.AddShopCartReq;
import us.wili.dev.client.model.req.DeleteReq;
import us.wili.dev.client.model.req.upShopCartReq;
import us.wili.dev.client.model.rsp.ShopcartRsp;
import us.wili.dev.client.service.ClientGoodsService;
import us.wili.dev.client.service.ClientShopCartService;
import us.wili.dev.client.service.ClientUserService;
import us.wili.dev.model.entity.Goods;
import us.wili.dev.model.entity.Order;
import us.wili.dev.model.entity.Shopcart;
import us.wili.dev.model.entity.User;
import us.wili.dev.model.example.OrderExample;
import us.wili.dev.model.example.ShopcartExample;
import us.wili.dev.service.impl.BaseShopCartServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClientShopCartServiceImpl extends BaseShopCartServiceImpl implements ClientShopCartService {
    @Resource
    private ClientShopCartMapper clientShopCartMapper;

    @Autowired
    private ClientUserService clientUserService;

    @Autowired
    private ClientGoodsService clientGoodsService;

    @Override
    public List<ShopcartRsp> shopcart(Long id) {
        User user = clientUserService.findUser(id);
        List<Shopcart> shopcarts = searShopCart(user);
        List<ShopcartRsp> rsp = new ArrayList<>();
        for (Shopcart shopcart : shopcarts) {
            Goods goods = clientGoodsService.shopcartgoods(shopcart);
            ShopcartRsp rsp1 = ShopcartRsp.ToShopCart(goods,shopcart);
            rsp.add(rsp1);
        }
        return rsp;
    }

    @Override
    public void delete(Long id) {
        ShopcartExample example = new ShopcartExample();
        example.createCriteria().andIdEqualTo(id);
        clientShopCartMapper.deleteByExample(example);
    }

    @Override
    public Shopcart Joinshopcart(AddShopCartReq req) {
        Shopcart shopcart = Goodss(req);
        if (shopcart==null){
            Shopcart shopcart1 = new Shopcart();
            shopcart1.setUserid(req.getId());
            shopcart1.setGoodsId(req.getGoodsId());
            shopcart1.setGoodsNum(req.getGoodsNum());
            clientShopCartMapper.insert(shopcart1);
            return shopcart;
        }
        shopcart.setGoodsNum(req.getGoodsNum());
        update(shopcart.getId(),shopcart,new ShopcartExample());
        return shopcart;
    }

    public Shopcart Goodss(AddShopCartReq req){
        ShopcartExample example = new ShopcartExample();
        example.createCriteria().andGoodsIdEqualTo(req.getGoodsId());
        return clientShopCartMapper.selectOneByExample(example);
    }

    @Override
    public Shopcart upshopcart(upShopCartReq req) {
        Shopcart shopcart = searShopCartfindid(req);
        shopcart.setGoodsNum(req.getGoodsNum());
        update(req.getId(),shopcart,new ShopcartExample());
        return shopcart;
    }

    @Override
    public void deletes(DeleteReq req) {
        if (req!=null){
            List<ShopCartId> id = JSON.parseArray(req.getId(), ShopCartId.class);
            for (ShopCartId jsonid : id) {
                ShopcartExample example = new ShopcartExample();
                example.createCriteria().andIdEqualTo(jsonid.getId());
                clientShopCartMapper.deleteByExample(example);
            }
        }
    }

    public Shopcart searShopCartfindid(upShopCartReq req){
        ShopcartExample example = new ShopcartExample();
        example.createCriteria().andIdEqualTo(req.getId());
        return clientShopCartMapper.selectOneByExample(example);
    }

    public List<Shopcart> searShopCart(User user){
        ShopcartExample example = new ShopcartExample();
        example.createCriteria().andUseridEqualTo(user.getId());
        return clientShopCartMapper.selectByExample(example);
    }

    protected int update(Long id,Shopcart shopcart,ShopcartExample example){
        example.createCriteria().andIdEqualTo(id);
        return clientShopCartMapper.updateByExampleSelective(shopcart,example);
    }
}
